package com.easy.controller;

import com.easy.bean.Order;
import com.easy.common.CommonResult;
import com.easy.dao.IInventoryDao;
import com.easy.bean.Inventory;
import com.easy.service.IInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/inventory")
public class InventoryController {
    @Autowired
    IInventoryService is;
    @Autowired
    IInventoryDao dao;
    @PutMapping("/editnumin")
    public CommonResult editInventoryInNum(@RequestBody Order order) {
        if (order == null || order.getList() == null || order.getList().isEmpty()) {
            return CommonResult.fail("订单列表为空");
        }
        for (Order item : order.getList()) {
            String productId = item.getProduct_id();
            int inNum;

            try {
                inNum = item.getIn_num(); // 将 in_num 转换为整数
            } catch (NumberFormatException e) {
                return CommonResult.fail("无效的库存数量: " + item.getIn_num());
            }

            // 获取当前库存
            Inventory existingInventory = is.getInventoryByProductId(productId);

            // 计算新的库存数量
            int newNumber = existingInventory.getNumber() + inNum;
            existingInventory.setNumber(newNumber);

            // 更新库存信息
            int count = dao.editInventory(existingInventory);
            if (count <= 0) {
                return CommonResult.fail("库存更新失败: " + productId);
            }
        }

        return CommonResult.success("库存更新成功");
    }
    @PutMapping("/editnumout")
    public CommonResult editInventoryOutNum(@RequestBody Order order) {
        if (order == null || order.getList() == null || order.getList().isEmpty()) {
            return CommonResult.fail("订单列表为空");
        }
        for (Order item : order.getList()) {
            String productId = item.getProduct_id();
            int outNum;

            try {
                outNum = item.getOut_num(); // 将 out_num 转换为整数
            } catch (NumberFormatException e) {
                return CommonResult.fail("无效的出库数量: " + item.getOut_num());
            }

            // 获取当前库存
            Inventory existingInventory = is.getInventoryByProductId(productId);
            if (existingInventory == null) {
                return CommonResult.fail("商品不存在: " + productId);
            }

            // 检查出库数量是否大于当前库存
            if (outNum <= 0) {
                return CommonResult.fail("出库数量必须大于零: " + productId);
            }
            if (outNum > existingInventory.getNumber()) {
                return CommonResult.fail("出库数量不能大于当前库存量: " + productId);
            }

            // 计算新的库存数量
            int newNumber = existingInventory.getNumber() - outNum;
            existingInventory.setNumber(newNumber);

            // 更新库存信息
            int count = dao.editInventory(existingInventory);
            if (count <= 0) {
                return CommonResult.fail("库存更新失败: " + productId);
            }
        }

        return CommonResult.success("库存更新成功");
    }

    //   分页

    @GetMapping("/paged")
    public CommonResult getPagedInventory(@RequestParam int page, @RequestParam int size) {
        List<Inventory> list = is.getPagedInventory(page, size);
        int total = is.getTotalInventoryCount();
        return CommonResult.success(list, total);
    }
    //  查询商品名或id
    @GetMapping("/search")
    public CommonResult searchInventory(@RequestParam String keyword){
        List<Inventory> inventories = is.searchInventory(keyword);
        return CommonResult.success(inventories);
    }

    @GetMapping("/all")
    public CommonResult getInventory() {
        List<Inventory> list = dao.getInventory();
        return CommonResult.success(list);
    }
    @GetMapping("/shelf")
    public CommonResult getShelfInventory() {
        List<Inventory> list = dao.getShelfInventory();
        return CommonResult.success(list);
    }

    @PostMapping("/add")
    public CommonResult addInventory(@RequestBody Inventory inventory){
        int count = dao.addInventory(inventory);
        if (count > 0) {
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }
    }

    @PutMapping("/edit/{product_id}")
    public CommonResult editInventory(@RequestBody Inventory inventory){
        int count = dao.editInventory(inventory);
        System.out.println(inventory+"------保存更新------");
        if (count > 0) {
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }
    }

    @DeleteMapping("/del/{product_id}")
    public CommonResult delInventory(@PathVariable String  product_id){
        int count = dao.delInventory(product_id);
        if (count > 0) {
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }
    }

    @DeleteMapping("/shelf/{product_id}")
    public CommonResult shelfInventory(@PathVariable String  product_id){
        int count = dao.shelfInventory(product_id);
        if (count > 0) {
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }
    }




}

